#include "CalcActivity.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
//#include "Product.h"
#include <QMessageBox>

/* class CalcActivity definition */

CalcActivity::CalcActivity(QWidget *parent, Bridget *bd) :
        QWidget(parent)

{
    calculateButton = new QPushButton(tr("Calculate"), this);
    addButton = new QPushButton(tr("Add new action"),this);
    inputActEdit = new QLineEdit(this);
    inputActEdit->setVisible(false);

    /*Buttons for added action button, are visible, when addButton is pressed */
    saveAdded = new QPushButton(tr("Ok"),this);
    cancel = new QPushButton(tr("Cancel"),this);
    saveAdded->setVisible(false);
    cancel->setVisible(false);

    //tittle label
   // lab = new QLabel(tr("<H3><CENTER> Activity calculator </CENTER></H3>"),this);

    /*Field for enter characteristics of the new action*, is visible, when
addButton is pressed*/
    inputEnergyLabel = new QLabel("Spend energy, kcal/h",this);
    inputEnergy = new QLineEdit(this);
    inputEnergyLabel->setVisible(false);
    inputEnergy->setVisible(false);

    inputActLabel =  new QLabel(" Action: ", this);
    inputAct = new QComboBox(this);

    inputTimeLabel = new QLabel(" Time, min: ", this);
    inputTime = new QLineEdit(this);
    QIntValidator *v = new QIntValidator(this);
    inputTime->setValidator(v);

    outputEnergyLabel = new QLabel(" Spent, Kcal ",this);


    outputEnergy = new QLineEdit (this);
    outputEnergy->setReadOnly(true);

    outputEnergyLabel->setVisible(false);
    outputEnergy->setVisible(false);

    act=new Act(bd);

    QFile file("baseAct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docAct.setContent(&file))
        {
            QDomElement root = docAct.documentElement();
            QDomElement child = root.firstChildElement("act");
            while (!child.isNull())
            {
                inputAct->addItem(child.attribute("name", ""));
                child = child.nextSiblingElement("act");

            }

        }

     file.close();
     }


    inputAct->setEditable("true");
   // inputProduct.setEditText("true");
    inputAct->clearEditText();


    QHBoxLayout *l4 = new QHBoxLayout;
    l4->addWidget(inputEnergyLabel);
    l4->addWidget(inputEnergy);



    QHBoxLayout *l5 = new QHBoxLayout();
    l5->addWidget(saveAdded);
    l5->addWidget(cancel);

    mainLayout = new QVBoxLayout(this);


    QHBoxLayout *l1 = new QHBoxLayout;
    l1->addWidget(calculateButton);
    l1->addWidget(addButton);


    //mainLayout->addWidget(lab);
    mainLayout->addWidget(inputActLabel);
//inputAct->showMinimized();
   // inputActLabel->setBuddy(inputAct);
    mainLayout->addWidget(inputAct);
    mainLayout->addWidget(inputActEdit);
    mainLayout->addWidget(inputTimeLabel/*,this->width()/3*/);
    mainLayout->addWidget(inputTime);
    mainLayout->addLayout(l1);
    mainLayout->addWidget(outputEnergyLabel);
    mainLayout->addWidget(outputEnergy);
    mainLayout->addWidget(inputEnergyLabel);
    mainLayout->addWidget(inputEnergy);
    mainLayout->addLayout(l5);
    mainLayout->addStretch();


 //   mainLayout->addWidget(&calculateButton);

   // mainLayout->addLayout(l3);



    connect(calculateButton, SIGNAL(clicked()),
            this, SLOT(onCalculateActivity()));

    connect(addButton, SIGNAL(clicked()),
            this, SLOT(onAddButton()));
    connect(saveAdded, SIGNAL(clicked()),
            this, SLOT(onSaveAddedAct()));
    connect(cancel, SIGNAL(clicked()),
            this, SLOT(onCancelButton()));

}

void CalcActivity::onCalculateActivity()
{

    if (inputAct->currentText() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input action."));
        return;
    }
    if (inputTime->text().size() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input time."));
        return;
    }
    if ((inputTime->text().toDouble() < 0)||(inputTime->text().toDouble()>2040))
    {
        QMessageBox::information(this, tr("Bridget"), tr("The enter time is not correct."));
        return;
    }

    act->setName(inputAct->currentText());
    act->setTime(inputTime->text().toDouble());
    if (act->setPar())
    {

        outputEnergy->setText(QString::number(act->getEnergy()));
    }
    else
    {
        QMessageBox::information(this, tr("Bridget"), tr("This action is absent in base."));
        return;
    }
    outputEnergyLabel->setVisible(true);
    outputEnergy->setVisible(true);

}


void CalcActivity::onAddButton()
{

    inputTimeLabel->setVisible(false);
    inputTime->setVisible(false);
    outputEnergy->setVisible(false);
    calculateButton->setVisible(false);
    addButton->setVisible(false);
    outputEnergyLabel->setVisible(false);
    saveAdded->setVisible(true);
    cancel->setVisible(true);
    inputEnergyLabel->setVisible(true);
    inputEnergy->setVisible(true);
    inputAct->setVisible(false);
    inputActEdit->setVisible(true);


}

void CalcActivity::onSaveAddedAct()
{
    if (inputActEdit->text().size() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No input action."));
        return;
    }
    if (inputEnergy->text().size() == 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("No spent energy."));
        return;
    }
    if (inputEnergy->text().toDouble() < 0)
    {
        QMessageBox::information(this, tr("Bridget"), tr("The spent energy isn't correct."));
        return;
    }


    act->setName(inputActEdit->text());
    act->setEnergy(inputEnergy->text().toDouble());


    if (act->inBase())
    {
        QMessageBox::information(this, tr("Bridget"), tr("This action is already in the base."));
    }
    else
    {
    act->addAct();
    inputAct->addItem(act->getName());

    QMessageBox::information(this, tr("Bridget"), tr("Action is added in base."));

}
    onCancelButton();
}

void CalcActivity::onCancelButton()
{
    saveAdded->setVisible(false);
    cancel->setVisible(false);
    inputEnergyLabel->setVisible(false);
    inputEnergy->setVisible(false);
    inputTimeLabel->setVisible(true);
    inputTime->setVisible(true);
    outputEnergy->setVisible(true);
    calculateButton->setVisible(true);
    addButton->setVisible(true);
    outputEnergyLabel->setVisible(true);
    inputAct->setVisible(true);
    inputActEdit->setVisible(false);
    outputEnergyLabel->setVisible(false);
    outputEnergy->setVisible(false);
    inputAct->setCurrentIndex(-1);
    inputTime->setText("");
   // inputTime->clearFocus();

}
